/*
	Returns the Ajax Object.
*/
function getXMLHttp() {
  var xmlHttp
  try {
    //Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
  } catch(e) {
    //Internet Explorer
    try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        alert("Your browser does not support AJAX!")
        return false;
      }
    }
  }
  return xmlHttp;
}


/*
	Updates the show state of the congress using ajax.
*/
function updateShowAbstract(idAbstract, show) {
  var xmlHttp = getXMLHttp();
  var url = "ajax_update_show_abstract.php?idAbstract=" + idAbstract + "&show=" + show;
  xmlHttp.open("GET", url, true);
  xmlHttp.send();
}

function logoutCongress() {
  var xmlHttp = getXMLHttp();
  var url = "congress_logout.php";
  xmlHttp.open("GET", url, true);
  xmlHttp.send();
}

function addNewSession(idcongress, sessionname, idSession) {
  var xmlHttp = getXMLHttp();

  xmlHttp.onreadystatechange = function() {
    if(xmlHttp.readyState == 4) {
      refreshSessionList(idcongress, idSession);
    }
  }

  if (sessionname.length == 0) {
	  alert('The session name cannot be empty');  
  } else if (confirm("Are you sure you want to add this new session?")) {
	  var url = "ajax_session_add.php?idCongress=" + idcongress + "&name=" + sessionname;
	  xmlHttp.open("GET", url, true);
	  xmlHttp.send();
  }
}

function refreshSessionList(idcongress, selectedSession ) {
  var xmlHttp = getXMLHttp();
  
  xmlHttp.onreadystatechange = function() {
    if(xmlHttp.readyState == 4) {
      document.getElementById('combosessiondiv').innerHTML=xmlHttp.responseText;
    }
  }
  
  var url = "ajax_session_combo.php?ID_CONGRESS=" + idcongress;
  if (selectedSession != -1) {
	url = url + "&ID_SESSION=" + selectedSession;  
  }
  xmlHttp.open("GET", url, false);
  xmlHttp.send();
}

function addNewAuthor(idcongress, name, organization, country) {
 var xmlHttp = getXMLHttp();
  
  xmlHttp.onreadystatechange = function() {
    if(xmlHttp.readyState == 4) {

   	  var authors = document.getElementById('availableAuthors');
	  var length = authors.options.length;
	  authors.options[length] = new Option(name, xmlHttp.responseText);

	}
  }
  
  var errors = false;
  
  if (name.length == 0) {
	alert("The name of the author cannot be empty");  
  } else if (organization.length == 0) {
	alert("The organization of the author cannot be empty");  
  } else if (country.length == 0) {
	alert("The country of the author cannot be empty");  
  } else if (confirm("Are you sure you want to add this new author?")) {
	  var url = "ajax_author_add.php?idCongress=" + idcongress + "&name=" + name + "&org=" + organization + "&country=" + country;
	  xmlHttp.open("GET", url, false);
	  xmlHttp.send();
  }
}